
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.condition;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import logger.PoolLogger;

import sun.awt.SunHints.Value;
import utility.ConvertToLower;
import database.Generatejoin;
import dbmanager.DBManager;
import dbmanager.GetIdName;
  

 
public class  Transaction_details implements Condition 
{	
	Vector<Object> Conditionvector=new Vector<Object>();
	String object;
	Hashtable<Object, Object> transaction_detailsmap = new Hashtable<Object, Object>();
	GetIdName gid;
	String QUERY;
	String TRANSACTION_DETAILS = "transaction_details";
	String id;
 /*	Vector<Object> TDField = new Vector<Object>();
 	Vector<Object> TDPLField = new Vector<Object>();*/
	PoolLogger pl;
	
	public Transaction_details() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	/**
	 * generate final buffer by logical operation of differnt condition	 
	 */
	@SuppressWarnings("unchecked")
	public void getResult() 
	{
		initializeData();
		Generatejoin gj =new Generatejoin();
		//gj.setDatabase(database);
		Vector<Object> MasterCondition =new Vector<Object>();
		Vector<Object> PLCondition =new Vector<Object>();
		
		for(int i=0;i<Conditionvector.size();i++)
		{
			try {
				Hashtable<Object, Object> table = new Hashtable<Object, Object>();
				table.putAll((Map<? extends Object, ? extends Object>) Conditionvector.get(i));
				
				if(transaction_detailsmap.containsKey(table.get(CONDITIONFIELD)))
				{
					if(transaction_detailsmap.get(table.get(CONDITIONFIELD)).equals("no"))
					{
						String field=(table.get(CONDITIONFIELD).toString());
						String value=table.get(CONDITIONVALUE).toString();
						table.remove(CONDITIONFIELD);
						table.remove(CONDITIONVALUE);
						table.put(field, value);										
						MasterCondition.add(table);
					}	
					else
					{
						String field=gid.getId(table.get(CONDITIONFIELD).toString());
						String value=table.get(CONDITIONVALUE).toString();
						value = gid.getItem(value);
						table.remove(CONDITIONFIELD);
						table.remove(CONDITIONVALUE);
						table.put(field, value);
						MasterCondition.add(table);
					}	
				}						
				else
					PLCondition.add(table);
			} catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		gj.setTableName("transaction_details", "td_property_details", MasterCondition, PLCondition, "id","tdid");
		QUERY =gj.JoinMasterPl(new Vector<Object>(),new Vector<Object>() );
	}
	/**
	 *  find condition,condition value, condition field,conditionvalue,
	 * logical operator,condition operator and put into Conditionmap
	 */
	public void initializeData() 
	{
		try 
		{
			ResultSet rs = DBManager.getSelect("SELECT fieldname,from_master FROM command where msgtype like '"+TRANSACTION_DETAILS+"' and process='add'" );
			while(rs.next())
			{
				String Fieldname=rs.getString("fieldname");
				//Fieldname=gid.getId(Fieldname);
				try 
				{
					transaction_detailsmap.put(Fieldname, rs.getString("from_master"));
				} catch (Exception e) {
					transaction_detailsmap.put(Fieldname, "no");
				}
			}
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
		}
		transaction_detailsmap = ConvertToLower.convertHashKey(transaction_detailsmap);
	}

	/**
	 * this function set database object and find master id of conditionfield,conditionvalue,
	 * @param db is database object
	 */
	public void initCondition() 
	{
		gid = new GetIdName();
	}
	/**
	 * set object
	 * @param Object is condition object
	 */
	public void setObject(String object) 
	{	
		this.object = object;
	}
	/**
	 * set buffer 
	 * @param buffer[] :String array 
	 */
	
	public static void main(String[] args) 
	{
		Transaction_details rate = new Transaction_details();
		rate.initCondition();
		rate.setObject("6286");
		rate.initializeData();
		rate.getResult();
		//String line[] = rate.getbuffer();
		/*for(int i=0;i<line.length;i++)
		{
			System.out.println(line[i]);
		}*/

	}
	/**
 	 *  find all record for given condition <p> when process is add in report_master then addreport is called</p>
	 */
	public void addReport() 
	{	
		Hashtable<Object, Object> table =new Hashtable<Object, Object>();
		table.put(CONDITIONFIELD, gid.getId("tid"));
		table.put(CONDITIONVALUE, id);
		table.put(CONDITIONOPERATOR, "=");
		Conditionvector.add(table);
		getResult();

	}
	/** 
	 * this return id which is present in report_master
	 * @return id is String  
	*/
	public String getId() 
	{
		return id;
	}
	/**
	 * set id which is present in report_master 
	 * @param id
	 */
	public void setid(String id) 
	{
		this.id = id;
	}
	/**
 	 *  find all record for given condition <p> when process is add in report_master then addreport is called</p>
	*/
	public void updateReport() 
	{
		getResult();
	}
	/**
	 * setConditionMap set condition map 
	 * @param Conditionmap  
	 */
	public void setConditionMap(Vector< Object> Conditionmap) 
	{
		this.Conditionvector = Conditionmap;
	}
	
	public String getQuery() 
	{
		return QUERY;
	}
	
	public void setbuffer(String buffer[]) {
		// TODO Auto-generated method stub
		
	}
	
	public void setQueryCount(int QueryCount) {
		// TODO Auto-generated method stub
		
	}
	public void setQuery(String Query) {
		// TODO Auto-generated method stub
		
	}
	
}

